package game.module.ddz.dao;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Files;

import game.module.ddz.bean.DdzRoomCfg;
import lion.common.Reloadable;

public class DdzRoomTemplateCache implements Reloadable {

	private static Logger logger = LoggerFactory.getLogger(DdzRoomTemplateCache.class);

	static class SingletonHolder {
		static DdzRoomTemplateCache instance = new DdzRoomTemplateCache();
	}

	public static DdzRoomTemplateCache getInstance() {
		return SingletonHolder.instance;
	}

	private volatile List<DdzRoomCfg> templateList;

	@Override
	public void reload() {
		try {
			String jsonStr = Files.toString(new File("data/ddzroom.json"), Charset.forName("UTF-8"));
			ObjectMapper mapper = new ObjectMapper();
			templateList = mapper.readValue(jsonStr, new TypeReference<List<DdzRoomCfg>>() {
			});
			logger.info("size={}", templateList.size());
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public boolean containsConfigIndex(int cfgIndex) {
		return cfgIndex < templateList.size();
	}

	public DdzRoomCfg getTemplate(int cfgIndex) {
		return (DdzRoomCfg) (templateList.get(cfgIndex));
	}

	public static void main(String[] args) {
		new DdzRoomTemplateCache().reload();
	}

}